﻿

#region Usings

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

#endregion

namespace Nasa8x.Core
{
    /// <summary>
    /// Utility class that does compression
    /// using GZip
    /// </summary>
    public static class GZip
    {
        #region Static Functions
        /// <summary>
        /// Compresses byte data
        /// </summary>
        /// <param name="Bytes">Byte array to be compressed</param>
        /// <returns>A byte array of compressed data</returns>
        public static byte[] Compress(byte[] Bytes)
        {
            MemoryStream Stream = new MemoryStream();
            GZipStream ZipStream = new GZipStream(Stream, CompressionMode.Compress, true);
            ZipStream.Write(Bytes, 0, Bytes.Length);
            ZipStream.Close();
            return Stream.ToArray();
        }

        /// <summary>
        /// Decompresses byte data
        /// </summary>
        /// <param name="Bytes">Byte array to be decompressed</param>
        /// <returns>A byte array of decompressed data</returns>
        public static byte[] Decompress(byte[] Bytes)
        {
            MemoryStream Stream = new MemoryStream();
            GZipStream ZipStream = new GZipStream(new MemoryStream(Bytes), CompressionMode.Decompress, true);
            byte[] Buffer = new byte[4096];
            while (true)
            {
                int Size = ZipStream.Read(Buffer, 0, Buffer.Length);
                if (Size > 0)
                {
                    Stream.Write(Buffer, 0, Size);
                }
                else
                {
                    break;
                }
            }
            ZipStream.Close();
            return Stream.ToArray();
        }


        public static string Compress(string text)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                zip.Write(buffer, 0, buffer.Length);
            }

            ms.Position = 0;
            // MemoryStream outStream = new MemoryStream();

            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);

            byte[] gzBuffer = new byte[compressed.Length + 4];
            Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
            return Convert.ToBase64String(gzBuffer);
        }

        public static string Decompress(string compressedText)
        {
            byte[] gzBuffer = Convert.FromBase64String(compressedText);
            using (MemoryStream ms = new MemoryStream())
            {
                int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                byte[] buffer = new byte[msgLength];

                ms.Position = 0;
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                {
                    zip.Read(buffer, 0, buffer.Length);
                }

                return Encoding.UTF8.GetString(buffer);
            }
        }
        #endregion
    }
}
